package cn.jekun.shop.web.action;

import javax.annotation.Resource;

import net.sf.json.JSONObject;
import cn.jekun.component.base.bean.ErrorMsg;
import cn.jekun.component.base.exceptions.BusinessException;
import cn.jekun.component.core.Constants;
import cn.jekun.shop.common.service.RegService;
import cn.jekun.shop.common.service.ShopBaseAction;
import cn.jekun.shop.web.service.AccountSafeService;
/**
 * 账户安全设置
 * @author twj
 *
 */
@SuppressWarnings("serial")
public class AccountSafeAction extends ShopBaseAction{
	@Resource(name = AccountSafeService.BEAN_NAME)
	protected AccountSafeService accountSafeService;
	@Resource(name = RegService.BEAN_NAME)
	RegService regService;
	/**
	 * 设置登录密码
	 * @return
	 */
	public String resetLoginPwd(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			//String userId = this.request.getParameter("userId");
			String userId = this.getUserId();
			String newPwd = this.request.getParameter("newPwd");
			String oldPwd = this.request.getParameter("oldPwd");
			jsonObject = accountSafeService.resetLoginPwd(userId,newPwd,oldPwd);
		} catch (BusinessException e) {
			// TODO: handle exception
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 设置邮箱
	 * @return
	 */
	public String setEmial(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String userId = this.getUserId();
			String email = this.request.getParameter("email");
			jsonObject = accountSafeService.setEmial(userId,email);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	
	public String setMobile(){
		JSONObject jsonObject = null;
		//验证是否登录
		if(!isLogin()){
			return timeout();
		}
		try{
			String userId = this.getUserId();
			String mobile = this.request.getParameter("mobile");
			String validateCode = this.request.getParameter("validateCode");
			jsonObject = accountSafeService.setMobile(userId,mobile,validateCode);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
			return this.writeJson(jsonObject);
		}
	/**
	 * 验证手机号是否注册
	 * @return
	 */
	public String validateMobile(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			jsonObject = regService.validateMobile(mobile);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	
	}
	/**
	 * 发送验证码
	 * @return
	 */
	public String sendValidateCode(){
		JSONObject jsonObject = null;
		try {
			String mobile = this.request.getParameter("mobile");//注册手机号
			jsonObject = regService.sendValidateCodeByUpdateMobile(mobile,Constants.RS_SMS_TYPE.UPDATE_MOBILE_CODE);
		} catch (BusinessException e) {
			jsonObject = JSONObject.fromObject(e.getErrorMsg());
		} catch (Exception e) {
			jsonObject = JSONObject.fromObject(new ErrorMsg(e.getMessage()));
			e.printStackTrace();
		}
		return this.writeJson(jsonObject);
	}
}
